<cfcomponent extends="assets.calgrants.com.Base">

	<cfscript>
        import assets.calgrants.com.service.CSACOrderService;
		this.service = new CSACOrderService();
	</cfscript>

	<cffunction name="get" output="false" access="remote" returntype="assets.calgrants.com.bo.CSACOrder">
		<cfreturn this.service.get(argumentCollection = arguments) />
	</cffunction>
	
	<cffunction name="query" output="false" access="public" returntype="query">
		<cfreturn this.service.read(argumentCollection = arguments) />
	</cffunction>
	
	<cffunction name="save" output="false" access="public" returntype="any">
		<cfscript>
			var csacOrder = "";
			var csacOrder_ = this.service.get(CSAC_Order_ID = arguments.CSAC_Order_ID);
			var orderGateway = "";
			var order = "";
			var order_ = "";
			var rollback = false;
			if (IsInstanceOf(arguments[1], "assets.calgrants.com.bo.CSACOrder")) {
				csacOrder = arguments[1];
			} else {
				var args = {};
				StructAppend(args, arguments);
				args.obj = Duplicate(csacOrder_);
				csacOrder = this.service.load(argumentCollection = args);
			}
		    var validator = getValidator();
		    var validationResults = validator.validate(csacOrder);
		    if (validationResults.getIsSuccess()) {
				csacOrder.setCSAC_Order_ID(this.service.save(csacOrder));
				// todo Remove Workshop_IDs property from CSACOrder
				saveCSACOrderWorkshops(CSAC_Order_ID = csacOrder.getCSAC_Order_ID(), Workshop_IDs = csacOrder.getWorkshop_IDs());
				if (StructKeyExists(args, "Order_ID")) {
					args.WMS_Order_ID = csacOrder.getCSAC_Order_ID();
					orderGateway = new assets.csac.com.gateway.OrderGateway();	
					order_ = orderGateway.get(Order_ID = args.Order_ID);
					order = orderGateway.save(argumentCollection = args);
					if (NOT IsInstanceOf(order, "assets.csac.com.bo.Order")) {
						// rollback csacOrder
						this.service.destroy(CSAC_Order_ID = csacOrder.getCSAC_Order_ID());
						csacOrder = "Order failed, rolled back";
					} else {
						// refresh csacOrder after Order/OrderProduct saved
						return this.service.get(CSAC_Order_ID = csacOrder.getCSAC_Order_ID());
					}
				}
				//order_ = orderGateway.get(Order_ID = csacOrder.getOrder_ID());
				//order = orderGateway.get(Order_ID = csacOrder.getOrder_ID());
				/*if (current.getApproved() eq 0 and order.getApproved() eq 1) {
					emailApprovalResult = emailOrderApproval(order.getOrder_ID());
					order.setFulfillment_Date(DateFormat(Now(), "m/d/yyyy"));
					this.service.save(order);
					emailFulfillmentResult = emailOrderToFulfillment(order.getOrder_ID());
				}
                return order;*/
            } else {
                return validationResults.getFailures();
            }
        </cfscript>
	</cffunction>
	
	<cffunction name="delete" output="false" access="public" returntype="boolean">
		<cfreturn this.service.destroy(argumentCollection = arguments) />
	</cffunction>

	<cffunction name="saveCSACOrderWorkshops" output="false" access="public" returntype="boolean">
		<cfargument name="CSAC_Order_ID" type="numeric" required="yes" />
		<cfargument name="Workshop_IDs" type="string" required="yes" />
		<cfset var DeleteCSACOrderWorkshops = "" />
		<cfset var workshopID = "" />
		<cfset var InsertCSACOrderWorkshop = "" />
		<cfquery name="DeleteCSACOrderWorkshops" datasource="#getDatasource()#">
			delete from CSAC_Order_Workshop
			where CSAC_Order_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.CSAC_Order_ID#">
		</cfquery>
		<cfloop index="workshopID" list="#arguments.Workshop_IDs#">
			<cfquery name="InsertCSACOrderWorkshop" datasource="#getDatasource()#">
				insert into CSAC_Order_Workshop
				(CSAC_Order_ID, 
				Workshop_ID)
				values
				(<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.CSAC_Order_ID#">,
				 <cfqueryparam cfsqltype="cf_sql_integer" value="#workshopID#">)
			</cfquery>
		</cfloop>
		<cfreturn true />
	</cffunction>

</cfcomponent>